package edu.pitt.cs.cs2510.project3;

/*
 * File: Main.java
 * Authors: Jesse Szwedko, Callen Shaw, Heather Friedberg
 * CS2510 Project 3
 */

import edu.pitt.cs.cs2510.project3.configuration.InputParameters;
import edu.pitt.cs.cs2510.project3.model.Simulator;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length < 2) {
			System.out.println("Content distribution network simulator:");
			System.out.println("usage: java Main <json-config-file> [s|d]");
			System.out.println("   where <json-config-file> defines system parameters,");
			System.out.println("   s specifies to run the static algorithm, and");
			System.out.println("   d specifies to run the dynamic algorithm");
			return;
		}

		String jsonFile = args[0];
		String mode = args[1].trim().toLowerCase();

		if(!mode.equals("s") && !mode.equals("d") && !mode.equals("t")){
			System.out.println("Algorithm Mode not specified correctly. Expects 's' or 'd', given '"+mode+"'");
			return;
		}

		InputParameters inputParameters = null;

		try
		{
			inputParameters = new InputParameters(jsonFile);
		}
		catch (Exception e)
		{
			System.err.println("Error loading configuration file:\n");
			e.printStackTrace();
			System.exit(1);
		}

		System.out.println("Configuration loaded successfully:\n"+inputParameters);
		
		Simulator simulator = null;
		
		if (mode.equals("s"))
		{
			simulator = new Simulator(inputParameters, Simulator.Algorithm.STATIC);
		}
		else if (mode.equals("d"))
		{
			simulator = new Simulator(inputParameters, Simulator.Algorithm.DYNAMIC);
		}
		else if (mode.equals("t"))
		{
			simulator = new Simulator(inputParameters, Simulator.Algorithm.TEST);
		}

		long time = System.currentTimeMillis();
		simulator.run();
		System.out.println(System.currentTimeMillis() - time);
	}
}
